package net.xdclass.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.model.OrderMessage;
import net.xdclass.service.IProductOrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@RabbitListener(queues = "${mqconfig.order_close_queue}")
@Component
public class ProductOrderMQListener {

    @Autowired
    private IProductOrderService productOrderService;

    @RabbitHandler
    public void closeProductOrder(OrderMessage orderMessage, Message message, Channel channel) throws IOException {

        log.info("监听到消息: closeProductOrder:{}", orderMessage);
        long msgTag = message.getMessageProperties().getDeliveryTag();

        try{

            boolean flag = productOrderService.closeProductOrder(orderMessage);
            if (flag){
                channel.basicAck(msgTag, false);
            }else {
                channel.basicReject(msgTag, true);
            }

        }catch (Exception e){
            log.error("定时关单失败：{}", orderMessage);
            channel.basicReject(msgTag, true);
        }

    }
}
